package com.gameGroups.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gameGroups.Dto.UserInfoDto;
import com.gameGroups.Dto.WorksDto;
import com.gameGroups.RedisImpl.RedisClientTemplate;
import com.gameGroups.Service.GiveLikeNumberService;
import com.gameGroups.Service.UpdateUserInfoService;
import com.gameGroups.Service.WorksAndArticleService;
import com.gameGroups.Utils.MyException;
import com.gameGroups.Utils.StringUtil;

@Controller
@RequestMapping("/user")
public class UpdateUserInfoController {
  @Autowired
  private UpdateUserInfoService updateUserInfoService;
  @Autowired
  private RedisClientTemplate redisClientTemplate;
  @Autowired
  private WorksAndArticleService worksAndArticleService;
  @Autowired
  private GiveLikeNumberService giveLikeNumberService;
  //查询用户信息
  @RequestMapping(value="/queryUserInfo.do",method=RequestMethod.POST)
  @ResponseBody
  public Map<String,Object> queryUserInfo(HttpServletRequest request,HttpServletResponse response) throws MyException{
	  Map<String,Object> model=new HashMap<String, Object>();
	try {
		 String uid = request.getParameter("uid");
		 UserInfoDto userInfo=updateUserInfoService.queryUserInfo(uid);
		 List<WorksDto>  worksList= updateUserInfoService.queryWorksList(uid);
		 if (StringUtil.isNullOrBlank(userInfo)) {
			 model.put("status", "5555"); 
			 model.put("errorMessage", "用户不存在！");
		 }else{
			 model.put("userInfo", userInfo);
			 List<WorksDto> worksUtil = worksUtil(worksList);
			 model.put("WorksList", worksUtil);
			 model.put("status", "0000"); 
		 }	
	} catch (Exception e) {
		model.put("status", "9999");
		model.put("errorMessage", "系统异常!");
		throw new MyException("查询用户信息功能异常!"+":"+e);
	}
	 return model;  
  }
  //更新用户信息
  @RequestMapping(value="/updateUserInfo.do",method=RequestMethod.POST)
  @ResponseBody
  public Map<String,Object> updateUserInfo(@RequestBody UserInfoDto userInfo,HttpServletRequest request,HttpServletResponse response) throws MyException{
	  Map<String,Object> model=new HashMap<String, Object>();
	  try {
		  int uid = userInfo.getUid();
		  String pName = userInfo.getpName();
		  updateUserInfoService.updateUserInfo(userInfo);
		  worksAndArticleService.updateWorks(uid,pName);
		  worksAndArticleService.updateWorksExt(uid,pName);
		  giveLikeNumberService.updateGiveLike(uid,pName);
		  model.put("status", "0000");
	} catch (Exception e) {
		model.put("status", "9999");
		model.put("errorMessage", "系统异常!");
		throw new MyException("更新用户信息功能异常!"+":"+e);
	}	 
	  return model;
  }
  
  //查询用户信息
  @RequestMapping(value="/findUid.do",method=RequestMethod.POST)
  @ResponseBody
  public Map<String,Object> findUserInfo(HttpServletRequest request,HttpServletResponse response) throws MyException{
	  Map<String,Object> model=new HashMap<String, Object>();
	  try {
		  String uid = request.getParameter("uid");
		  UserInfoDto userInfo = updateUserInfoService.queryUserInfo(uid);
		  model.put("userInfo", userInfo);
		  model.put("status", "0000");
	} catch (Exception e) {
		model.put("status", "9999");
		model.put("errorMessage", "系统异常!");
		throw new MyException("查询用户信息功能异常!"+":"+e);
	}	 
	  return model;
  }
  
  public List<WorksDto> worksUtil(List<WorksDto> worksList){
	   List<WorksDto> works=new ArrayList<WorksDto>();
		  for (WorksDto work :worksList) {
			  String worksImg = work.getWorksImg();
			  String[] split = worksImg.split(",");
			   work.setWorksImg(split[0]);
			   works.add(work);
		    }
		 return works;
  }
  
  
  //查询用户信息
  @RequestMapping(value="/checkLite",method=RequestMethod.POST)
  @ResponseBody
  public Map<String,Object> checkLite(HttpServletRequest request,HttpServletResponse response) throws MyException{
	  Map<String,Object> model=new HashMap<String, Object>();
	  String code = request.getParameter("code");
	  if ("100".equals(code)) {
		  model.put("isLite", true);
	   }else{
		  model.put("isLite", false);
	   }
	  return model;
  }  
}
